from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()


# 用户表
class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    name = db.Column(db.String(32), comment='用户名')
    mobile = db.Column(db.String(11), comment='手机号')
    role = db.relationship('RoleModel', backref='role')


# 角色表
class RoleModel(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='角色ID')
    name = db.Column(db.String(32), comment='角色名')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))


# 资源表
class ResourceModel(db.Model):
    __tablename__ = 'resource'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='资源ID')
    name = db.Column(db.String(32), comment='资源名')


# 角色资源表
role_resource = db.Table('role_resource',
                         db.Column('roleid', db.Integer, db.ForeignKey('role.id'), primary_key=True),
                         db.Column('resourceid', db.Integer, db.ForeignKey('resource.id'), primary_key=True)
                         )


# vip表
class VipModel(db.Model):
    __tablename__ = 'vip'
    id = db.Column(db.Integer, primary_key=True, comment='vipID')
    name = db.Column(db.String(32), comment='vip等级')
    condition = db.Column(db.String(128), comment='条件')
    users = db.relationship('UsersModel', backref='users')


# 用户表
class UsersModel(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, comment='用户ID')
    name = db.Column(db.String(32), comment='用户名')
    url = db.Column(db.String(256), comment='头像')
    vip = db.Column(db.String(32), comment='vip等级')
    vip_id = db.Column(db.Integer, db.ForeignKey('vip.id'))


# 标签表
class LabelModel(db.Model):
    __tablename__ = 'label'
    id = db.Column(db.Integer, primary_key=True, comment='标签ID')
    name = db.Column(db.String(32), comment='标签名')


# 用户标签表
user_label = db.Table('user_label',
                      db.Column('usersid', db.Integer, db.ForeignKey('users.id'), primary_key=True),
                      db.Column('labelid', db.Integer, db.ForeignKey('label.id'), primary_key=True)
                      )


# 创建者表
class CreatorModel(db.Model):
    __tablename__ = 'creator'
    id = db.Column(db.Integer, primary_key=True, comment='创建表ID')
    money = db.Column(db.String(32), comment='标签名')


# 机构表
class OrganizationModel(db.Model):
    __tablename__ = 'organization'
    id = db.Column(db.Integer, primary_key=True, comment='机构表ID')
    name = db.Column(db.String(30), comment='单位名称')
    rank = db.Column(db.String(30), comment='创造级别')


# 主播表
class AnchorModel(db.Model):
    __tablename__ = 'anchor'
    id = db.Column(db.Integer, primary_key=True, comment='机构表ID')
    money = db.Column(db.Integer,default=0, comment='总收益')


# 视频分类表
class VisualModel(db.Model):
    __tablename__ = 'visual'
    id = db.Column(db.Integer, primary_key=True, comment='频道ID')
    name = db.Column(db.String(32),comment='视频分类名称')
    pid = db.Column(db.Integer, db.ForeignKey("channel_model.id"))


# 频道表
class ChannelModel(db.Model):
    __tablename__ = 'channel'
    id = db.Column(db.Integer, primary_key=True, comment='频道ID')
    name = db.Column(db.String(32), comment='频道名')
    visual = db.relationship("VisualModel", backref='channel')


# 长视频表
class LongformvideoModel(db.Model):
    __tablename__ = 'longformvideo'
    id = db.Column(db.Integer, primary_key=True, comment='长视频ID')
    mold = db.Column(db.String(32), comment='类型')


# 帖子分类表
class Classification(db.Model):
    __tablename__ = 'classification'
    id = db.Column(db.Integer, primary_key=True, comment='帖子分类ID')
    name = db.Column(db.String(32),comment='分类名')
    invitation = db.relationship("InvitationModel",backref='classification')

# 帖子表
class InvitationModel(db.Model):
    __tablename__ = 'invitation'
    id = db.Column(db.Integer, primary_key=True, comment='帖子ID')
    name = db.Column(db.String(32),comment='帖子名称')
    likes = db.Column(db.Integer,default=0,comment='点赞量')
    classification_id = db.Column(db.Integer,db.ForeignKey("classification_model.id"))